$(function() {
	// var today = new Date();
	// var year_today = today.getFullYear();
	// var month_today = (today.getMonth() + 1) < 10 ? "0" + (today.getMonth() + 1) : (today.getMonth() + 1);
	// var day = today.getDate() < 10 ? "0" + today.getDate() : today.getDate();

	//获取服务器当前时间
	var http = new XMLHttpRequest();
	http.open("HEAD", ".", false);
	http.send(null);
	var today = new Date(http.getResponseHeader("Date"));
	var year_today = today.getFullYear();
	var month_today = (today.getMonth() + 1) < 10 ? "0" + (today.getMonth() + 1) : (today.getMonth() + 1);
	var day = today.getDate() < 10 ? "0" + today.getDate() : today.getDate();
	var nextMonth = "";
	if(month_today == 12) {
		nextMonth = "01";
	}else{
		nextMonth = (today.getMonth() + 2) < 10 ? "0" + (today.getMonth() + 2) : (today.getMonth() + 2);
	}
    // console.info("nextMonth=" + nextMonth);
	

	$("#currentYear").click(function() {
		init(year_today);
	});
	$("#lastYear").click(function() {
		var cerrentYear = $("#year").html();
		var showYear = parseInt(cerrentYear) -1;
		init(showYear);
	});
	$("#nextYear").click(function() {
		var cerrentYear = $("#year").html();
		var showYear = parseInt(cerrentYear) + 1;
		init(showYear);
	});

	//给每月添加点击事件
	$("td[name='month']").click(function(){
		if($('#state').val()=="试用"){
			bootbox.alert("试用期/实习期员工不在绩效考核范围内！");
			return;
		}
		var year = $("#year").html();
		var month = $(this).attr('value');
		var timeStr = year + "-" + month;

		//去除前后空格
		timeStr = timeStr.replace(/(^\s*)|(\s*$)/g, "");
		// console.info("timeStr:",timeStr);
		$.ajax({
			url: '/api/performance/singlePerf/' + timeStr + '?',
			type: 'get',
			data: {'_t': new Date().getTime()},
			success: function(result) {
				console.info(result);
				var data = result.rows;
				var access = result.access;
				if(data != null && data.length != 0) {
					//绩效已确认跳转到绩效查看页面，否则跳转到绩效填写页面
					if(data[0].state == 2) {
						window.location.href = '/performance?year=' + year + '&month=' + month;
						// if(year != year_today || month != month_today) {
						// 	window.location.href = '/performance?year=' + year + '&month=' + month;
						// }
						
					}else if(data[0].state == 0){
						//当月的23-26号可以修改当月及下个月的绩效目标
						if((year_today == year && month_today == month && ((day >= 23 && day <= 26) || access == "填写")) || (year_today == year && nextMonth == month && ((day >= 23 && day <= 26) || access == "填写") )) {
							window.location.href = '/performanceAdd?year=' + year + '&month=' + month + '&access=' + access;
						}else if(month_today == 12) {
							if((year_today + 1) == year && month == 01 && ((day >= 23 && day <= 26) || access == "填写")) {
								window.location.href = '/performanceAdd?year=' + year + '&month=' + month + '&access=' + access;
							}else{
								bootbox.alert("每月的23-26号才能填写/修改当月绩效目标及填写下月绩效目标");
							}
						}else{
							bootbox.alert("每月的23-26号才能填写/修改当月绩效目标及填写下月绩效目标");
						}
						
						
						
					}
				}else{
					//当月的23-26号填写当月或下个月的绩效目标
					if((year_today == year && month_today == month && ((day >= 23 && day <= 26) || access == "填写")) || (year_today == year && nextMonth == month && ((day >= 23 && day <= 26) || access == "填写") )) {
							window.location.href= '/performanceAdd?year=' + year + '&month=' + month + '&access=' + access;
					}else if(month_today == 12) {
						if((year_today + 1) == year && month == 01 && ((day >= 23 && day <= 26) || access == "填写")) {
							window.location.href = '/performanceAdd?year=' + year + '&month=' + month + '&access=' + access;
						}else{
							bootbox.alert("每月的23-26号才能填写/修改当月绩效目标及填写下月绩效目标");
						}
					}else{
						bootbox.alert("每月的23-26号才能填写/修改当月绩效目标及填写下月绩效目标");
					}
					
		           

				}
				
								
			}
		});

	});

	//初始化月历数据
	var init = function(year) {
		// console.info("year=" + year);
		var tds = $("td[name='month']");
		//清空月历
		$.each(tds, function(i, obj) {
			var galMonth = $(obj).attr('value');
			// console.info(galMonth);
			//可点击月份光标设定为手形
			if(year == year_today && galMonth == month_today) {
				$(obj).attr('style','width:25%;word-wrap:break-word;cursor:pointer;');
			}else if(month_today == 12 && year == (year_today + 1) && galMonth == 01) {
				$(obj).attr('style','width:25%;word-wrap:break-word;cursor:pointer;');
			}else if(year == year_today && galMonth == nextMonth) {
				$(obj).attr('style','width:25%;word-wrap:break-word;cursor:pointer;');
			}else{
				$(obj).attr('style', 'width:25%;word-wrap:break-word;');
			}
			
			
			var monId = "message" + galMonth;
			// var j = i + 1;
			// if(j < 10) {
			// 	monId = "message0" + j;
			// }else{
			// 	monId = "message" + j;
			// }
			
			$("#"+monId).html("");
		});
		if(year == null || year == "") {
			year = $("#year").html().replace(/(^\s*)|(\s*$)/g, "");
		}else{
			$("#year").html(year);
		}
		
		$.ajax({
			url: '/api/performance/selEmpPerfs?',
			type: 'get',
			data: {'year':year, '_t':new Date().getTime()},//添加时间变量_t解决ie的get请求缓存问题
			success: function(data) {
				// console.info(data);
				if(data != null && data.length != 0) {
					
					var uncheckMonth = [];
					var checkMonth = [];
					//当月绩效是否已填写标志
					var flag = false;
					//下月绩效是否已填写
					var nextFlag = false;
					$.each(data, function(i, item) {
						
						var perfDate = item.perfDate;
						var dateArr = perfDate.split("-");
						//当月绩效已填写
						
						if(dateArr[0] == year_today && dateArr[1] == month_today) {
							flag = true;
						}
						if(month_today == 12) {
							if(dateArr[0] == (year_today + 1) && dateArr[1] == "01") {
								nextFlag = true;
							}
						}else{
							if(dateArr[0] == year_today && dateArr[1] == nextMonth) {
								nextFlag = true;
							}
						}
						var monthId = "message" + dateArr[1];
							if(item.state == 0) {
								
								$("#"+monthId).html("待审核");
								uncheckMonth.push(dateArr[1]);
							}else if(item.state == 1 || item.state == 3){
								$("#"+monthId).html("审核中");
								uncheckMonth.push(dateArr[1]);
								// if(year == year_today && dateArr[1] == month_today) {
								// 	$("#"+monthId).html("审核中");
								// 	uncheckMonth.push(dateArr[1]);
								// }else{
								// 	$("#"+monthId).html("成绩：" + item.score + "</br>" + "等级：" + item.level);
								// 	checkMonth.push(dateArr[1]);
								// }
								
								
							}else{
								$("#"+monthId).html("成绩：" + item.score + "</br>" + "等级：" + item.level);
								checkMonth.push(dateArr[1]);
							}
						
						
					});
					
					if(!flag && year == year_today) {
						var flagMonth = "message" + month_today;
						$("#" + flagMonth).html("绩效目标未填写");
					}
					
					
					// var tds = $("td[name='month']");
					$.each(tds, function(i, obj) {
						var month_td = $(obj).attr('value');
						var nextFlagMonth = "message" + nextMonth;
						if(month_today == 12) {
							if(!nextFlag && year == (year_today + 1) && month_td == nextMonth) {
								$("#" + nextFlagMonth).html("请填写下月绩效目标");
							}
						}else{
							if(!nextFlag && year == year_today && month_td == nextMonth) {
								$("#" + nextFlagMonth).html("请填写下月绩效目标");
							}
						}
						if(!flag && year == year_today) {
							if($(obj).attr('value') == month_today) {
								$(obj).attr('style', 'width:25%;word-wrap:break-word;background:#FFC1C1;cursor:pointer;');
							}
						}
						
						for(var k = 0; k < uncheckMonth.length; k++) {
							
							if($(obj).attr('value')==uncheckMonth[k]) {
								
								$(obj).attr('style', 'width:25%;word-wrap:break-word;background:#ffc;cursor:pointer;');
							}
						}
						for(var m = 0; m < checkMonth.length; m++) {
							if($(obj).attr('value')==checkMonth[m]) {
								$(obj).attr('style', 'width:25%;word-wrap:break-word;background:#ecf2f7;cursor:pointer;');
							}
						}
					});
				}else{
					$.each(tds, function(i, obj) {
						var month_td = $(obj).attr('value');
						var nextFlagMonth = "message" + nextMonth;
						if(year == year_today) {
							var flagMonth = "message" + month_today;
							$("#" + flagMonth).html("绩效目标未填写");
							if($(obj).attr('value') == month_today ) {
								$(obj).attr('style', 'width:25%;word-wrap:break-word;background:#FFC1C1;cursor:pointer;');
							}
						}
						if(month_today == 12) {
							if(year == (year_today + 1) && month_td == nextMonth) {
								$("#" + nextFlagMonth).html("请填写下月绩效目标");
							}
						}else{
							if(year == year_today && month_td == nextMonth) {
								$("#" + nextFlagMonth).html("请填写下月绩效目标");
							}
						}

						
					});
					
				}
			}
		});
	};

   
	init(year_today);



});